package com.example.huwang.activity;

import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.huwang.R;
import com.example.huwang.adapter.CaseAdapter;
import com.example.huwang.adapter.HistoryAdapter;
import com.example.huwang.model.HistoryBean;
import com.example.huwang.model.LoginBean;
import com.example.huwang.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.huwang.app.MainApplication.dbController;
import static com.example.huwang.app.MainApplication.dbHistoryController;

public class HistoryActivity extends BaseActivity{
    @BindView(R.id.return_btn)
    public ImageView returnBtn;
    @BindView(R.id.return_text)
    public TextView returnText;
    @BindView(R.id.title)
    public TextView title;
    @BindView(R.id.recyclerview)
    public RecyclerView recyclerview;

    List<HistoryBean> historyBeanList;
    HistoryAdapter historyAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        //绑定初始化ButterKnife
        ButterKnife.bind(this);

        dbHistoryController.deleteAll();

        insert();
        queryAllUser();
        initview();
    }

    private void initview() {
        title.setText("历史记录");
        returnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        returnText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        historyAdapter = new HistoryAdapter(historyBeanList);
        //设置适配器
        recyclerview.setAdapter(historyAdapter);
        historyAdapter.setOnClikListener(new HistoryAdapter.OnClikListener() {
            @Override
            public void onClick(View view, int position) {
                Log.e("****", "position:" + position);
                Toast.makeText(HistoryActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                dbHistoryController.delete(historyBeanList.get(position).getName());
//                dbHistoryController.deleteAll();
//                queryAllUser();
                historyBeanList.remove(position);
                Log.e("****", "historyBeanList.size():" + historyBeanList.size());
                historyAdapter.notifyDataSetChanged();
            }
        });
    }

    private void insert() {
        if (SPUtils.getInstance().isFirstAddHistory()) {
            HistoryBean historyBean = new HistoryBean();
            historyBean.setTitle("超级用户于淘宝存在受骗危险");
            historyBean.setTime("2022-03-22");
            historyBean.setName("超级用户1");
            HistoryBean historyBean2 = new HistoryBean();
            historyBean2.setTitle("超级用户于QQ存在受骗危险");
            historyBean2.setTime("2022-03-22");
            historyBean2.setName("超级用户2");
            HistoryBean historyBean3 = new HistoryBean();
            historyBean3.setTitle("超级用户于微博存在受骗危险");
            historyBean3.setTime("2022-03-22");
            historyBean3.setName("超级用户3");
            historyBeanList = new ArrayList<>();
            historyBeanList.add(historyBean);
            historyBeanList.add(historyBean2);
            historyBeanList.add(historyBean3);
            // 插入新用户
            dbHistoryController.insert(historyBean);
            dbHistoryController.insert(historyBean2);
            dbHistoryController.insert(historyBean3);
            SPUtils.getInstance().setIsFirstAddHistory(false);
        }
    }

    private void queryAllUser() {
        historyBeanList = dbHistoryController.searchAll();

        Log.e("****", "historyBeanList.size():" + historyBeanList.size());
    }
}
